/**
 * @file     hasCycle.js
 * @brief    [141. 环形链表](https://leetcode-cn.com/problems/linked-list-cycle/comments/)
 * @author   Zhu
 * @date     2022-03-17 00:18
 */

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * 快慢指针
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
    let slow = head;
    let fast = head;
    do {
        fast = fast?.next?.next ?? null;
        slow = slow?.next ?? null;
    } while (slow !== fast && fast !== null);

    if (fast === null) return false;

    return true;
};
